/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it
 */
function newXMLHttpRequest() {

  var xmlreq = false;

  if (window.XMLHttpRequest) {

    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();

  } else if (window.ActiveXObject) {

    // Create XMLHttpRequest via MS ActiveX
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer

      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e1) {

      // Failed to create required ActiveXObject

      try {
        // Try version supported by older versions
        // of Internet Explorer

        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (e2) {

        // Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }

  return xmlreq;
}


/*
 * Adds an item, identified by its product code, 
 * to the shopping cart
 * itemCode - product code of the item to add.
 */
function getFileDetails(fileName) {

  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandler(req, updateImportArea);
  req.onreadystatechange = handlerFunction;
  
  // Open an HTTP POST connection to the shopping cart servlet.
  // Third parameter specifies request is asynchronous.
  req.open("POST", "file", true);

  // Specify that the body of the request contains form data
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

  // Send form encoded data stating that I want to add the 
  // specified item to the cart.
  req.send("elib_action=READ&file_name="+fileName);
}

/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its XML response
 * to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 */
function getReadyStateHandler(req, responseXmlHandler) {

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {

    // If the request's status is "complete"
    if (req.readyState == 4) {
      
      // Check that a successful server response was received
      if (req.status == 200) {

        // Pass the XML payload of the response to the 
        // handler function
        responseXmlHandler(req.responseXML);

      } else {

        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
    }
  }
}

function updateImportArea(fileXML)
{

 var publications = fileXML.getElementsByTagName("data")[0];

   // Loop over the items in the cart
   var items = publications.getElementsByTagName("item");
   
   for (var I = 0 ; I < items.length ; I++)
   {
     var item = items[I];

     // Extract the text nodes from the name and quantity elements
     var name      = item.getElementsByTagName("name")[0].firstChild.nodeValue;
     var title = name.substring(0, name.length - 4)
     
     var quantity  = item.getElementsByTagName("pages")[0].firstChild.nodeValue;
     var thumbnail = item.getElementsByTagName("preview")[0].firstChild.nodeValue;
     var pdf_link  = item.getElementsByTagName("document")[0].firstChild.nodeValue;

     var frm = document.getElementById("import-form");

     frm.elements['title'].value = title;
     frm.elements['pages'].value = quantity;

     displayThumbnail(thumbnail);
     
     displayPDF(pdf_link);
     
     console.dir(thumbnail);
   }
}


function displayThumbnail(file)
{
	var thumb = document.getElementById("file-import-thumbnail");
    thumb.src = file;
}

function displayPDF(file)
{
	var dsplTag = document.getElementById("pdf-display-object");
	
	if (dsplTag != null)
    {
		dsplTag.setAttribute('src', file);
    }
	// var success = new PDFObject({ url: file }).embed("file-import-display");
}